using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Derrick.Web
{
    public class Request
    {
        public string Method;
        public string Url;
        public string Version;
        public string Body;
        public int ContentLength;
        public Dictionary<string, string> Headers = new Dictionary<string, string>();

        private TraceSwitch _traceSwitch = new TraceSwitch("TraceLevelSwitch", "COMET Request.");

        public bool HasContent()
        {
            if (Headers.ContainsKey("Content-Length"))
            {
                ContentLength = int.Parse(Headers["Content-Length"]);
                return true;
            }
            return false;
        }

        public bool VerifyAuth(AuthConfig auth)
        {

            //return true; //added by DLau - June 5, 2012

            if (auth.Type == AuthType.Basic)
            {
                string[] pair;
                if (Headers.ContainsKey("Authorization"))
                {
                    pair = Headers["Authorization"].Split(' ');
                    if (pair.Length == 2)
                    {
                        //string temp = Util.DecodeBase64(pair[1]);
                        //if (pair[0] == "Basic" && Util.DecodeBase64(pair[1])  == auth.Username + ":" + auth.Password)
                        //{
                        //    return true;
                        //}
                        //else
                        //{
                        //    return false;
                        //}

                        if (!pair[0].Equals("Basic"))
                        {
                            return false;
                        }

                        string usernamePasswordPair = Util.DecodeBase64(pair[1]);
                        if (String.IsNullOrEmpty(usernamePasswordPair))
                        {
                            return false;
                        }
                        Trace.WriteLineIf(_traceSwitch.TraceVerbose, "Client username/password pair is [" + usernamePasswordPair + "].");
                        if (usernamePasswordPair.Equals(auth.Username + ":" + auth.Password))
                        {
                            return true;
                        }
                        return false;

                    }
                    else
                    {
                        return false;
                    }

                }
                else
                {
                    Trace.WriteLineIf(_traceSwitch.TraceVerbose, "Client header doesn't contain Authorization key.");
                    return false;
                }
            }
            else if (auth.Type == AuthType.Digest)
            {
                //TODO: Handle digest authenticaion
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
